<template>
  <div class="login">
    <div class="login_box">
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <el-form-item>
          <h1>协同OA自动化办公系统后台管理</h1>
        </el-form-item>
        <!-- 用户名 -->
        <el-form-item prop="phone">
          <el-input
            v-model="loginForm.phone"
            placeholder="请输入账号"
            prefix-icon="el-icon-user"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            type="password"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button class="button" type="primary" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  data() {
    return {
      //登录表单的数据绑定对象
      loginForm: {
        phone: "13987332016",
        password: "123456",
      },
      //表单验证规则对象
      loginFormRules: {
        //验证用户名是否合法
        phone: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个字符",
            trigger: "blur",
          },
        ],
        //验证用户密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      role1: [],
      jobnumber: {},
    };
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        //   const { data: res } = await this.$http.post("/login", qs.stringify(this.loginForm), {
        //     headers: { "Content-Type": "application/x-www-form-urlencoded" },
        //   });
        //   console.log(res);
        //   if (res.flag !== true) return this.$message.error("登录失败!");
        //   this.$message.success("登录成功!");
        //   this.$router.push("/home");
        //   window.sessionStorage.setItem("token", res.data.token);
        // });
        this.$http({
          method: "post",
          url: "/login",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },

          data: qs.stringify(this.loginForm),
        }).then((res) => {
          console.log(res);
          if (res.status !== 200) return this.$message.error("登录失败!");
          this.$message.success("登录成功!");
          //将token值保存到sessionStorage中
          window.sessionStorage.setItem("token", res.headers.authorization);
          // window.sessionStorage.setItem("role", res.data.authorities);
          this.$router.push("/home");
          //将拿到的role值通过sessionStorage传到其它页面
          this.role = res.data.authorities[0];
          console.log(this.role);
          let roleStr = JSON.stringify(this.role);
          sessionStorage.setItem("rolee", [roleStr]);
          console.log(sessionStorage.getItem("rolee"));
          //将拿到的role值通过sessionStorage传到其它页面
          this.jobnumber = res.data.principal.jobNumber;
          console.log(this.jobnumber);
          let jobnumberStr = JSON.stringify(this.jobnumber);
          sessionStorage.setItem("jobNumberx", [jobnumberStr]);
          console.log(sessionStorage.getItem("jobNumberx"));
        });
      });
    },
  },
};
</script>

<style scoped>
.login {
  background-image: url("~@/assets/bj.png");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}
.login_box {
  height: 400px;
  width: 350px;
  background: white;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.login_form {
  top: 20px;
  position: absolute;
  bottom: 0;
  width: 250px;
  padding: 0 50px;
  box-sizing: content-box;
}
.button {
  width: 250px;
  font-size: 20px;
  font-family: "Microsoft soft";
  background-image: url("~@/assets/button.png");
  background-size: cover;
  border-radius: 100px/95px;
}
</style>
